var machineModel,machineSerialNo,outTimeStart,outTimeEnd,buyer,creater,status=-1;
var serverUrl=getPath();
$(function(){
	queryClass();
	page.bind();
});
var page = { //页面总变量
	_this_selectDialog:"",
	_this_selectDialog_val:"",
	bind:function(){
		$("#search").unbind("click");
		$("#search").bind("click.my",function(){
			machineModel=$("#machineModel").val();
			machineSerialNo=$("#machineSerialNo").val();
			outTimeStart=$("#outTimeStart").val();
			outTimeEnd=$("#outTimeEnd").val();
			buyer=$("#buyer").val();
			creater=$("#creater").val();
			status=$("#status").val();
			queryClass();
		})
		$(window.parent.document).find("span[data-href='workorderlist.html']").parent().unbind('click.my');
		$(window.parent.document).find("span[data-href='workorderlist.html']").parent().bind('click.my',function(){
			reloadData();
		})
		$("#delete_order_sure").unbind("click.my");
		$("#delete_order_sure").bind("click.my",function(){
			page.deletesure();
		})
		//添加设备 出现dialog
		$("#addMachine").unbind("click");
		$("#addMachine").bind("click",function(){
			page.addMachine();
		})
		//保存设备
		$("#addMachineSubmit").unbind("click");
		$("#addMachineSubmit").bind("click",function(){
			$("#addMachineSubmit").hide();
			$("#form-addMachine").ajaxSubmit(function(data){
				if(data.result){
					alert("保存成功");
					$("#addMachine-modal-demo").find(".close").click();
					reloadData();
				}else{
					alert(data.info);
				}
				$("#addMachineSubmit").show();
				
				
			});
		})
		//更新设备
		$("#updateMachineSubmit").unbind("click");
		$("#updateMachineSubmit").bind("click",function(){
			$("#updateMachineSubmit").hide();
			$("#form-updateMachine").ajaxSubmit(function(data){
				if(data.result){
					alert("更新成功");
					$("#updateMachine-modal-demo").find(".close").click();
					reloadData();
				}else{
					alert(data.info);
				}
				$("#updateMachineSubmit").show();
			});
		});
		//主管 和 技工绑定事件
		$("[name='serviceManager']").unbind("click");
		$("[name='serviceManager']").bind("click",function(){
			page._this_selectDialog=this;
			page.selectDialog();
		})
		$("[name='serviceWorker']").unbind("click");
		$("[name='serviceWorker']").bind("click",function(){
			page._this_selectDialog=this;
			page.selectDialog();
		})
		$("[id='factoryNoText']").unbind("click");
		$("[id='factoryNoText']").bind("click",function(){
			page._this_selectDialog=this;
			page.selectOrderDialog();
		})
	},
	//选择人员
	selectDialog:function(){
		$('div#easyui_dialog_index').dialog({
            modal: true,
            title: '人员设备',
            width: "500",
            height: "400",
            closed: false,
            border:true,
            cache: false,
            content:"<iframe frameborder='0' scrolling='no' style='width:99%;height:97%;' src='userSelectDialog.html'></iframe>",
            onClose : function() {  
               $("div#easyui_dialog_index").dialog('destroy');  
               $("body").append("<div id='easyui_dialog_index'></div>");
            },
        	buttons: [{
	            text:'确定',
	            iconCls:'icon-ok',
	            handler:function(){
	            	var rows = $("[src='userSelectDialog.html']")[0].contentWindow.getSelectRows();
	            	if(rows.length<1){
	                	alert("请设置人员");
	                	return;
	                }
	            	var str="";
	            	for(i=0;i<rows.length;i++){
	            		str+=rows[i].text+","
	            	}
	            	str=str.substring(0,str.length-1);
	            	$(page._this_selectDialog).val(str);
	            	$("div#easyui_dialog_index").dialog('destroy');  
	                $("body").append("<div id='easyui_dialog_index'></div>");
	            }
	        }]
        });
		//赋值
		page._this_selectDialog_val=$(page._this_selectDialog).val();
	},
	selectOrderDialog:function(){
		$('div#easyui_dialog_index').dialog({
            modal: true,
            title: '关联订单',
            width: "500",
            height: "400",
            closed: false,
            border:true,
            cache: false,
            content:"<iframe frameborder='0' scrolling='no' style='width:99%;height:97%;' src='orderSelectDialog.html'></iframe>",
            onClose : function() {  
               $("div#easyui_dialog_index").dialog('destroy');  
               $("body").append("<div id='easyui_dialog_index'></div>");
            },
        	buttons: [{
	            text:'确定',
	            iconCls:'icon-ok',
	            handler:function(){
	            	var rows = $("[src='orderSelectDialog.html']")[0].contentWindow.getSelectRows();
	            	if(rows.length<1){
	                	alert("请设置人员");
	                	return;
	                }
	            	var str="";
	            	for(i=0;i<rows.length;i++){
	            		str+=rows[i].text+","
	            	}
	            	str=str.substring(0,str.length-1);
	            	$(page._this_selectDialog).val(str);
	            	$(page._this_selectDialog).parent().find("[id='connectOrder']").val(rows[0].id);
	            	$("div#easyui_dialog_index").dialog('destroy');  
	                $("body").append("<div id='easyui_dialog_index'></div>");
	            }
	        }]
        });
		//赋值
		page._this_selectDialog_val=$(page._this_selectDialog).parent().find("[id='connectOrder']").val();
	},
	//添加设备
	addMachine:function(){
		$("#addMachine-modal-demo").modal("show");
	},
	//更新设备
	updateMachine:function(self){
		$("#updateMachine-modal-demo").modal("show");
		var table = $('#attendanceClassTable').DataTable();
		var data;
		setTimeout(function(){
			data = table.row('.selected').data();
			$("#form-updateMachine").find("[name='id']").val(data.id);
			$("#form-updateMachine").find("[name='machineModel']").val(data.machineModel);
			$("#form-updateMachine").find("[name='machineSerialNo']").val(data.machineSerialNo);
			$("#form-updateMachine").find("[name='outTime']").val(data.outTime);
			$("#form-updateMachine").find("[name='buyer']").val(data.buyer);
			$("#form-updateMachine").find("[name='status']").val(data.status);
			$("#form-updateMachine").find("[name='serviceManager']").val(data.serviceManager);
			$("#form-updateMachine").find("[name='serviceWorker']").val(data.serviceWorker);
			$("#form-updateMachine").find("[name='orderNo']").val(data.orderNo);
			$("#form-updateMachine").find("[id='factoryNoText']").val(data.factoryNo);
			$("#form-updateMachine").find("[name='internetThingsCard']").val(data.internetThingsCard);
		},200)
	},
	//二维码
	qrcode:function(id){
		$("#qrcode-modal-demo").modal("show");
		$('#qrcodeDIV').empty();
		var qrcode=$('#qrcodeDIV').qrcode({//生成二维码
            render      : "canvas",
            text        : serverUrl+"/workOrder/sqr?id="+id,
            render      : "canvas",//设置渲染方式
            width       : $("#qrcodeDIV").width(),     //设置宽度
            height      : $("#qrcodeDIV").height(),     //设置高度
            typeNumber  : -1,      //计算模式
            //correctLevel    : QRErrorCorrectLevel.H,//纠错等级
            background      : "#ffffff",//背景颜色
            //src: '../../../images/zfb_logo.jpg',
            foreground      : "#000000" //前景颜色
         });  
		var canvas=qrcode.find('canvas').get(0);
		$('#qrcodeDIV').hide();
		$('#imgOne').attr('src',canvas.toDataURL('image/jpeg'))
	}
}
function reloadData(){
	var table = $('#attendanceClassTable').DataTable();
	table.draw(false);
}
//查询
function queryClass(){
	//避免多次绑定
	var table = $('#attendanceClassTable').DataTable();
	$('#attendanceClassTable tbody').unbind("click");
	ajaxUtil.dataTablesAjax("attendanceClassTable",10,"workOrder/getConfigList",
			{"machineModel":machineModel,
			 "machineSerialNo":machineSerialNo,
			 "outTimeStart":outTimeStart,
			 "outTimeEnd":outTimeEnd,
			 "buyer":buyer,
			 "status":status
			},
			[{"data":"id"},
			{"data":"factoryNo"},
			{"data":"machineModel"},
			{"data":"machineSerialNo"},
			{"data":"outTime"},
			{"data":"buyer"},
			{"data":"createTime"},
			{"data":"creater"},
			{"data":"serviceManager"},
			{"data":"serviceWorker"},
			{"data":"internetThingsCard"},
			{"data":"status",render:function(data,type,row){
				if(data==0){
					return "禁用";
				}
				if(data==1){
					return "启用";
				}
			}},
			{"data":"id",render:function(data,type,row){
				return '<input class="btn btn-link" type="button" value="打开下载"  onclick="page.qrcode('+data+')">';
			}},
			{"data":"id",render:function(data,type,row){
				return '<input class="btn btn-link" type="button" value="更新"  onclick="page.updateMachine()">';
			}}
			
	]);
	$('#attendanceClassTable').on( 'init.dt', function ( e, settings ) {
		layer.msg("查询完成",{icon:1});
		page.bind();
	});
	$('#attendanceClassTable tbody').on( 'click', 'tr', function () {
		$('#attendanceClassTable tbody').find("tr").removeClass('selected');
		$(this).addClass('selected');
    } );
}
/**
 * loadFilter: function (data, parent) {
 *  var opt = $(this).data().treegrid.options;
 *  return treeFilter(data, opt);
 * }
 * 
 * idField : 'id'
 * treeField : 'text'  //必须指定
 * parentField : 'pid' //必须指定
 * @param data
 * @param opt
 * @returns
 */
function treeFilter(data, opt) {
    var idField,
    treeField,
    parentField;
    if (opt.parentField) {
        idField = opt.idField || 'id';
        treeField = opt.treeField || 'text';
        parentField = opt.parentField;

        var i,
        l,
        treeData = [],
        tmpMap = [];

        for (i = 0, l = data.length; i < l; i++) {
            tmpMap[data[i][idField]] = data[i];
        }

        for (i = 0, l = data.length; i < l; i++) {
            if (tmpMap[data[i][parentField]] && data[i][idField] != data[i][parentField]) {
                if (!tmpMap[data[i][parentField]]['children'])
                    tmpMap[data[i][parentField]]['children'] = [];
                data[i]['text'] = data[i][treeField];
                tmpMap[data[i][parentField]]['children'].push(data[i]);
            } else {
                data[i]['text'] = data[i][treeField];
                treeData.push(data[i]);
            }
        }
        return treeData;
    }
    return data;
}
function getPath(){
	var strFullPath=window.document.location.href;  
    var strPath=window.document.location.pathname;  
    var pos=strFullPath.indexOf(strPath);  
    var prePath=strFullPath.substring(0,pos);  
    var postPath=strPath.substring(0,strPath.substr(1).indexOf('/')+1);  
    var basePath = prePath;  
    basePath = prePath + postPath;
    return basePath;
}
function getPrePath(){
	var strFullPath=window.document.location.href;  
    var strPath=window.document.location.pathname;  
    var pos=strFullPath.indexOf(strPath);  
    var prePath=strFullPath.substring(0,pos);  
    var postPath=strPath.substring(0,strPath.substr(1).indexOf('/')+1);  
    var basePath = prePath;  
    return basePath;
}
setInterval(function(){
	$(".dataTables_scroll").contents().scroll(function(event){
		$(".dataTables_scroll").contents().scrollTop($(this).scrollTop());
		$(".dataTables_scroll").contents().scrollLeft($(this).scrollLeft()) ;
	});
},1000);
/*
$("iframe[src='./html/createOrder/index_x.html']")[0].contentWindow.basic_index.getTempForOrder('182','SC180308000010')*/




